package com.urbanski.clipboard.client.action;

import org.apache.log4j.Logger;
import com.urbanski.clipboard.client.ClipboardClient;
import com.urbanski.clipboard.client.util.ClientConstants.ClientCommand;
import com.urbanski.util.logging.LoggingUtil;

public class SetAliasActionHandler extends ClientActionHandler
{
	private static final Logger LOGGER = Logger.getLogger(SetAliasActionHandler.class);
	
	private String server;
	private String newAlias;

	public SetAliasActionHandler(String[] tokens)
	{
		super(ClientCommand.SET_ALIAS, tokens);
	}

	@Override
	public void execute()
	{
		processArgs();
		if (server != null && newAlias != null)
		{
			if (newAlias.equals("-r"))
			{
				newAlias = null;
			}
			ClipboardClient.setAlias(server, newAlias);
		}
	}
	
	private void processArgs()
	{
		if (tokens.length != 3)
		{
			LoggingUtil.logAndPrintError(LOGGER, "Invalid number of arguments for setalias command");
			return;
		}
		
		server = tokens[1];
		newAlias = tokens[2];
	}

}
